'use strict';
const {redis_set_Notices} =  require('../../../utils/init')
const Controller = require('../../core/base_controller');

class NoticeController extends Controller {


  // 修改岗位状态
  async updateStatus() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { status } = ctx.request.body;

    const rules = {
      status: {
        type: 'enum',
        values: [0, 1, true, false],
        required: true,
        message: '状态值错误',
      },
    };

    try {
      ctx.validate(rules);
    } catch (err) {
      const errors = err.errors.map(error => rules[error.field].message).map(el=>el.string).join();
      ctx.throw(400, errors);
    }

    const notice = await ctx.model.Notice.findByPk(id);
    if (!notice) {
      ctx.throw(404, '岗位不存在');
    }
    await notice.update({ status });
    redis_set_Notices(ctx.app)
    this.success(null, '状态更新成功');
  }


  // 获取公告列表
  async index() {
    const { ctx } = this;
    const { page = 1, pageSize = 10, title, status, sort } = ctx.query;

    const where = {};
    if (title) where.title = { [this.app.Sequelize.Op.like]: `%${title}%` };
    if (status !== undefined) where.status = status;

    const order = [];
    if (sort !== undefined) {
      order.push(['sort', 'ASC']);
    }
    order.push(['created_at', 'DESC']);

    const result = await ctx.model.Notice.findAndCountAll({
      where,
      order,
      attributes : {exclude: ['content','sort','status']},
      offset: (page - 1) * pageSize,
      limit: parseInt(pageSize),
    });

    this.success({
      list: result.rows,
      pagination: {
        total: result.count,
        current: parseInt(page),
        pageSize: parseInt(pageSize),
      },
    });
  }

  // 获取公告列表
  async detail() {
    const { ctx } = this;
     const {id} = ctx.params
    const result = await ctx.model.Notice.findByPk(id);
    this.success(result);
  }


  // 创建公告
  async create() {
    const { ctx } = this;
    const { title, content, sort = 0, status = 1 } = ctx.request.body;

    ctx.validate({
      title: { type: 'string', required: true },
      content: { type: 'string', required: false },
      sort: { type: 'number', required: false },
      status: { type: 'number', required: false },
    });

    const notice = await ctx.model.Notice.create({
      title,
      content,
      sort,
      status,
    });
    redis_set_Notices(ctx.app)
    this.success(notice);
  }

  // 更新公告
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const { title, content, sort, status } = ctx.request.body;

    ctx.validate({
      title: { type: 'string', required: true },
      content: { type: 'string', required: false },
      sort: { type: 'number', required: false },
      status: { type: 'number', required: false },
    });

    const notice = await ctx.model.Notice.findByPk(id);
    if (!notice) {
      this.error('公告不存在');
      return;
    }

    await notice.update({
      title,
      content,
      sort,
      status,
    });
    redis_set_Notices(ctx.app)
    this.success(notice);
  }

  // 删除公告
  async destroy() {
    const { ctx } = this;
    const id = ctx.params.id;

    const notice = await ctx.model.Notice.findByPk(id);
    if (!notice) {
      this.error('公告不存在');
      return;
    }

    await notice.destroy();
    redis_set_Notices(ctx.app)
    this.success();
  }
  // 批量更新排序
  async updateSort() {
    const { ctx } = this;
    const { sorts } = ctx.request.body;

    ctx.validate({
      sorts: { type: 'array', required: true },
      'sorts.id': { type: 'number', required: true },
      'sorts.sort': { type: 'number', required: true },
    });

    for (const item of sorts) {
      await ctx.model.Notice.update(
        { sort: item.sort },
        { where: { id: item.id } }
      );
    }
    redis_set_Notices(ctx.app)
    this.success();
  }
}

module.exports = NoticeController;
